﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class admin_Modules_vsDetail_Edit : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        this.btnAddNew.Attributes.Add("onclick", "return OnSubmitClick();");
        if (!this.IsPostBack) {
            InitItem();
            LoadCategory();
        }
    }
   
    protected void btnAddNew_Click(object sender, EventArgs e)
    {
        AddNew();
        
    }

    private void LoadCategory()
    {
        using (WebData db = new WebData())
        {
            this.cbbidCat.DataSource = db.tbl_CatTagss.GetAll();
            this.cbbidCat.DataBind();
        }

    }

    private void AddNew()
    {
        String strMess = "";
        int id = 0;
        int ID = -1;
        try
        {
            ID = Convert.ToInt32(Request.Params["ID"].ToString());
        }
        catch { }
        try
        {

            using (WebData db = new WebData())
            {
                DataLibrary.tbl_DetailCate news = new DataLibrary.tbl_DetailCate();
                news.IdCat = Convert.ToInt32(cbbidCat.SelectedValue);
                news.Title = txtTieude.Text;
                news.IsActive = 1;
                news.OrderId = Convert.ToInt32(txtorderId.Text);
                news.IdLang = 1;
                news.Description = txtNoidung.Text;
                news.IdDetail = ID;
                id = db.tbl_DetailCates.UpdateOnSubmit(news);
            }

        }
        catch (Exception err)
        {
            strMess = Message.Insert_err + "! vì lỗi " + err.Message.ToString();
        }
        ScriptManager.RegisterStartupScript(this, this.GetType(), "Err", "alert('" + strMess + "');", true);
        if (id != -1)
            Response.Redirect(ViewState["URLReferrer"].ToString());

    }

    private void InitItem()
    {
        int ID=-1;
        try {
            ID = Convert.ToInt32(Request.Params["ID"].ToString());
        }
        catch { }
        ViewState["URLReferrer"] = Request.UrlReferrer.AbsoluteUri;
        using (WebData db = new WebData()) {
            DataLibrary.tbl_DetailCate item = db.tbl_DetailCates.GetItemById(ID);
            if (item != null) {
                txtTieude.Text = item.Title;
                txtNoidung.Text = item.Description;
                txtorderId.Text = item.OrderId.ToString();
            }
        }
    }


    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect(ViewState["URLReferrer"].ToString());
    }
}